import { assign } from 'min-dash';

export default class CustomContextPad {
	constructor(config, contextPad, create, elementFactory, injector, translate, bpmnFactory, modeling) {
		this.create = create;
		this.elementFactory = elementFactory;
		this.translate = translate;
		this.bpmnFactory = bpmnFactory;
		this.modeling = modeling;

		if (config.autoPlace !== false) {
			this.autoPlace = injector.get('autoPlace', false);
		}

		contextPad.registerProvider(this); // 定义这是一个contextPad
	}

	getContextPadEntries(element) {
		const { autoPlace, create, elementFactory, translate, bpmnFactory, modeling } = this;

		// const taskId = element.businessObject.taskId || element.id

		function appendTask(event, element) {
			if (autoPlace) {
				const businessObject = bpmnFactory.create('bpmn:Task');
				businessObject['taskId'] = 'BlankNode' + '_' + window.aiWorkFlowlId++;
				const shape = elementFactory.createShape({
					type: 'bpmn:Task',
					id: businessObject['taskId'],
					businessObject
				});
				// autoPlace.append(element, shape);
				const x0 = element.x;
				// const y0 = element.y
				const width0 = element.width;
				// const height0 = element.height
				var newShape = autoPlace.append(element, shape);
				const x1 = newShape.x;
				// const y1 = newShape.y
				const width1 = newShape.width;
				const height1 = newShape.height;
				// console.log('appendTask : ', element, newShape);
				let y = x1 - x0 - width0 + height1;
				if (element.type === 'bpmn:StartEvent') {
					y = height1;
				}
				modeling.moveElements([newShape], { x: x0 - x1 - (width1 - width0) / 2, y: y });
			} else {
				appendTaskStart(event, element);
			}
		}

		function appendTaskStart(event) {
			const shape = elementFactory.createShape({ type: 'bpmn:Task' });
			create.start(event, shape, element);
		}

		var actions = {};
		// 开始节点，只有没有子节点时才能加空白节点
		if (element.id === 'StartEvent_1') {
			if (element.outgoing.length <= 0) {
				assign(actions, {
					'append.blank-node': {
						group: 'model',
						className: 'icon-custom-context blank-node',
						title: translate('空白节点'),
						action: {
							click: appendTask,
							dragstart: appendTaskStart
						}
					}
				});
			}
		}
		// else if (element.type !== "bpmn:SequenceFlow" &&
		//     !taskId.includes('BlankNode') &&
		//     !taskId.includes('transfer') &&
		//     !taskId.includes('hangup')
		// ) {
		//   assign(actions, {
		//     'append.blank-node': {
		//         group: 'model',
		//         className: 'icon-custom-context blank-node',
		//         title: translate('空白节点'),
		//         action: {
		//             click: appendTask,
		//             dragstart: appendTaskStart
		//         }
		//     }
		//   });
		// }
		return actions;
	}
}

CustomContextPad.$inject = ['config', 'contextPad', 'create', 'elementFactory', 'injector', 'translate', 'bpmnFactory', 'modeling'];
